﻿using System;
using System.Linq;
using Lbb.Data;
using Lbb.Web.Mvc;
using Lbb.URating.DL;
using Lbb.Common;

namespace Lbb.URating.Models
{
    /// <summary>
    /// 评分分类数据列表
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickCategoryDataList : ExtDataList<U_ClickCategory>
    {
        #region Ctors
        /// <summary>
        /// 构建评分分类数据列表
        /// </summary>
        public U_ClickCategoryDataList()
        {
            if (Request != null)
            {
                ObjectTypesStr = Request["objTypes"];
                AllowRedo = GetReqBool("allowRedo");
            }
        }
        #endregion Ctors

        #region Props

        #region ObjectTypes
        /// <summary>
        /// 关联的对象类型
        /// </summary>
        public string ObjectTypesStr
        {
            get
            {
                return RouteValues.objTypes as string;
            }
            set
            {
                RouteValues.objTypes = value;
                objTypesChanged = true;
            }
        }
        private bool objTypesChanged = false;
        /// <summary>
        /// 关联的对象类型
        /// </summary>
        public string[] ObjectTypes
        {
            get
            {
                if (objTypes == null || objTypesChanged)
                {
                    objTypes = ObjectTypesStr.Split(removeEmpty: true);
                }
                return objTypes;
            }
            set
            {
                ObjectTypesStr = value.Merger();
                objTypesChanged = false;
            }
        }
        private string[] objTypes;
        #endregion ObjectTypes

        /// <summary>
        /// 允许重新评分
        /// </summary>
        public bool? AllowRedo
        {
            get
            {
                return RouteValues.allowRedo as bool?;
            }
            set
            {
                RouteValues.allowRedo = value;
            }
        }

        #endregion Props

        #region Methods
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="data">数据源</param>
        public void FillData(IQueryable<U_ClickCategory> data)
        {
            if (data == null) throw new ArgumentNullException("data");

            switch (OrderBy)
            {
                case "title_up":
                    data = data.OrderBy(d => d.Title);
                    break;
                case "title_down":
                    data = data.OrderByDescending(d => d.Title);
                    break;
                case "objType_up":
                    data = data.OrderBy(d => d.ObjectType);
                    break;
                case "objType_down":
                    data = data.OrderByDescending(d => d.ObjectType);
                    break;
                case "allowRedo_up":
                    data = data.OrderBy(d => d.AllowRedo).OrderBy(d => d.AllowRedo != null);
                    break;
                case "allowRedo_down":
                    data = data.OrderByDescending(d => d.AllowRedo).OrderBy(d => d.AllowRedo != null);
                    break;
            }

            Items = data.GetPartialData(FromIndex, PageSize, out Total);
        }
        #endregion Methods
    }
}
